﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;

namespace Ccit.SysPlat.Organ
{
    /// <summary>
    /// Description: 平台组织机构 -> 机构信息维护
    /// Author: tim 2010-01-28
    /// </summary>
    public partial class sys_organ_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidOrganId.Value = (null != Request.QueryString["organId"]) ? Request.QueryString["organId"].ToString() : "1";
                initializePage();
            }
        }

        private void initializePage()
        {
            initAddNew();

            OrganBLL organBLL = new OrganBLL();
            string organId = _hidOrganId.Value;
            MsOrgan msOrgan = organBLL.GetOrganInfo(organId);
            initEdit(msOrgan);

            msOrgan = null;
            organBLL.Release();
            organBLL = null;
        }

        private void initAddNew()
        {
            txtOrganName.Text = string.Empty;
            txtOrganCode.Text = string.Empty;
            txtDesc.Text = string.Empty;
        }

        private void initEdit(MsOrgan msOrgan)
        {
            if (null == msOrgan) return;

            txtOrganName.Text = msOrgan.organName;
            txtOrganCode.Text = msOrgan.organCode;
            txtDesc.Text = msOrgan.organDesc;
        }


        #region ajax methods

        /// <summary>
        /// 保存机构信息
        /// </summary>
        /// <param name="jsonExpr">机构信息表达式</param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string SaveOrgan(string jsonExpr,string ctrlType)
        {
            OrganBLL organBLL = new OrganBLL();
            MsOrgan msOrgan = JavaScriptConvert.DeserializeObject(jsonExpr, typeof(MsOrgan)) as MsOrgan;
            List<string> lstResult = organBLL.Save(msOrgan);            
            var newObj = new {
                OPERTYPE = "SAVE",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1],
                CTRLTYPE = ctrlType
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            lstResult = null;
            newObj = null;
            msOrgan = null;
            organBLL.Release();
            organBLL = null;
            return serExpr;
        }
        #endregion
    }
}
